<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="Your description goes here" />
        <meta name="keywords" content="your,keywords,goes,here" />
        <meta name="author" content="Your Name" />
        <link rel="stylesheet" type="text/css" href="index.css" title="Variant Duo" media="screen,projection" />
        <title>myEdit</title>
</head>

<body>

<?
  $curFile  = $_SESSION['curFile'];
  $curDir   = $_SESSION['curDir'];


$fileType = $_GET['ft'];

if($fileType == 'dir')
{
	$name = $_GET['name'];
        $name = $curDir.'/'.$name;
	chdir($name);
  	$curDir = getcwd();
        $_SESSION['curDir'] = $curDir;
}

if($fileType == 'fil')
{
	$curFile = $_GET['name'];
        $_SESSION['curFile'] = $curFile;
}

if (isset($_POST['syscom']))
{
  $syscom = $_POST['syscom'];
}
if (isset($_POST['filename']))
{
  $filename  = $_POST['filename'];
  $_SESSION['filename'] = $filename;
}


if (isset($_POST['save_file']))
{
 $content = $_POST['content'];
 $target = $curDir.'/'.$curFile;
 $fp = fopen($target, 'w');
 if($fp)
  fwrite($fp,$content);
 else
  echo("Could not open scenario ($target) (write)!");
 fclose($fp);
}

if($syscom)
{
  $syscom = $syscom.' > syscom.myEdit';
  echo("$syscom");
  system($syscom);
}

if($syscom)
{
  $in = fopen('syscom.myEdit',"r");
  if($in)
    {
      echo("<br>---------------<br>");
      while (!feof($in))
	{
	  $row = fgets($in);
	  echo("$row<br>");
        }
     echo("---------------<br>");
    }
 else
   echo("Error: Could not open syscom file<br>");
fclose($in);
}


if($curFile)
{
 $target = $curDir.'/'.$curFile;
//echo("open $target<br>");
 $handle = fopen($target, "r");
 $content = fread($handle, filesize($target));
 fclose($handle);
}


$dir = $curDir;

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(is_dir($file))
              echo("*<a href=\"index.php?ft=dir&name=$file\">$file</a><br>");
            else
              echo("<a href=\"index.php?ft=fil&name=$file\">$file</a><br>");
        }
        closedir($dh);
    }
}

  echo("Current Directory: $curDir<br>");
  echo("$filename<form name=\"form_save_file\" action=\"index.php\" method=\"post\"> ");
  echo("<input type=\"hidden\" name=\"save_file\" value=\"$filename\">");
  echo("<textarea name=\"content\" cols=80 rows=40>$content</textarea>");
  echo("<input type =\"submit\" name=\"submit_save_file\" value=\"".Save."\">");
  echo("</form>");

  echo("<form name=\"form_syscom\" action=\"index.php\" method=\"post\"> ");
  echo("<input type=\"text\" size=\"30\" name=\"syscom\" value=\"\">");
  echo("<input type =\"submit\" name=\"submit_syscom\" value=\"".Exec."\">");
  echo("</form>");

  echo("<form name=\"form_edit\" action=\"index.php\" method=\"post\"> ");
  echo("<input type=\"text\" size=\"30\" name=\"filename\" value=\"\">");
  echo("<input type =\"submit\" name=\"submit_filename\" value=\"".Edit."\">");
  echo("</form>");
?>

</body>
